﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace ABDLight.Components
{
    public partial class frmReplace : Form
    {
        DataGridView grd;
        String field;
        int Type = 0;
        public frmReplace()
        {
            InitializeComponent();
            cmbPos.SelectedIndex = 0;
        }

 public frmReplace(DataGridView _grd,string _field)
        {
            InitializeComponent();
            grd = _grd;
            field = _field;
          
            if (((DataView)grd.DataSource).Table.Columns[field].DataType ==
                     System.Type.GetType("System.String"))
            {
            Type=0;
            if (this.cmbPos.Items.Count == 0)
            {
                this.cmbPos.Items.AddRange(new object[] {
                "С начала",
                "С конца",
                "Везде"});
            }
            }
            /*if (((DataView)grd.DataSource).Table.Columns[field].DataType ==
                 System.Type.GetType("System.Boolean"))
            {
                Type = 1;
                lbPos.Text="Заменить:";
                edSearch.Enabled = false;
                edReplace.Enabled = false;
                
             this.cmbPos.Items.AddRange(new object[] {
            "Заменть Да на НЕТ",
            "Заменть Нет на Да",
            "Поменять на обратное"});

            }
            else {
                Type = 2;
                this.cmbPos.Items.AddRange(new object[] {
            "=",
            "!="});
            }
             */ 
            cmbPos.SelectedIndex = 2;


        }

        private void btnOk_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < ((DataView)grd.DataSource).Table.Rows.Count; i++)
            {
                Type = 0;
                if (Type==0)
                {
                    string val = ((DataView)grd.DataSource).Table.Rows[i][field].ToString();
                    string sval = (chRegister.Checked) ? sval = val : sval = val; 
                        if (cmbPos.SelectedIndex == 0)
                        {

                            //if (sval.Substring(0, edSearch.Text.Length) == ((chRegister.Checked) ? edSearch.Text.ToLower() : edSearch.Text.ToLower()))
                            //{
                                //((DataView)grd.DataSource).Table.Rows[i][field] = edReplace.Text + val.Substring(0, edSearch.Text.Length);
                                //Console.WriteLine("{0}", ((DataView)grd.DataSource).Table.Rows[i][field].ToString());
                            val = Regex.Replace(val, "^" + edSearch.Text.Replace(".", @"\."), edReplace.Text, ((chRegister.Checked) ? System.Text.RegularExpressions.RegexOptions.IgnoreCase : System.Text.RegularExpressions.RegexOptions.None));
                            //}
                            if (val != "")
                            {
                                ((DataView)grd.DataSource).Table.Rows[i][field] = val;
                            }                         
                        }
                        else if (cmbPos.SelectedIndex == 1)
                        {
                            val = Regex.Replace(val, edSearch.Text.Replace(".", @"\.") + "$", edReplace.Text, ((chRegister.Checked) ? System.Text.RegularExpressions.RegexOptions.IgnoreCase : System.Text.RegularExpressions.RegexOptions.None));
                            if (val != "")
                            {
                                ((DataView)grd.DataSource).Table.Rows[i][field] = val;
                            }
                        }
                        else if (cmbPos.SelectedIndex == 2)
                        {
                            val = Regex.Replace(val, edSearch.Text.Replace(".",@"\."), edReplace.Text, ((chRegister.Checked) ? System.Text.RegularExpressions.RegexOptions.IgnoreCase : System.Text.RegularExpressions.RegexOptions.None));
                            if (val != "")
                            {
                                ((DataView)grd.DataSource).Table.Rows[i][field] = val;
                            }
                        }
                }
                else if (Type == 1)
                {

                        if (cmbPos.SelectedIndex == 0)
                        {
                            if ((bool)((DataView)grd.DataSource).Table.Rows[i][field] == true) ((DataView)grd.DataSource).Table.Rows[i][field] = false;
                        }
                        else if (cmbPos.SelectedIndex == 1)
                        {
                            if ((bool)((DataView)grd.DataSource).Table.Rows[i][field] != false) ((DataView)grd.DataSource).Table.Rows[i][field] = true;
                        }
                        else { 
                         ((DataView)grd.DataSource).Table.Rows[i][field] =!(bool)((DataView)grd.DataSource).Table.Rows[i][field];
                        }

                }
                else if(Type==2) {

                   
                        Int32 src;
                        Int32 dst;
                        Int32.TryParse(edSearch.Text,out src);
                         Int32.TryParse(edReplace.Text,out dst);
                         if (cmbPos.SelectedIndex == 0) { 
                         if ((Int32)((DataView)grd.DataSource).Table.Rows[i][field] == src) ((DataView)grd.DataSource).Table.Rows[i][field] = dst;
                         }else{
                         if ((Int32)((DataView)grd.DataSource).Table.Rows[i][field] != src) ((DataView)grd.DataSource).Table.Rows[i][field] = dst;
                    }
                
                
                }
            }
            
        }

        private void edReplace_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
